import { defineStore } from 'pinia';

// 创建并导出 userStore
export const useLoginUserStore = defineStore('user', {
  state: (): API.LoginUserVO => ({
    userName: undefined,
    avatarUrl: undefined,
    phone: undefined,
    email: undefined,
    id: undefined,
    createTime: undefined,
    updateTime: undefined,
    openid: undefined,
    userRole: undefined
  }),
  actions: {
    setUserName(name: string) {
      this.userName = name;  // 修改状态
    },
    setAvatarUrl(url: string) {
      this.avatarUrl = url;  // 修改状态
    },
    setPhone(phone: string) {
      this.phone = phone;  // 修改状态
    },
    setEmail(email: string) {
      this.email = email;  // 修改状态
    },
    setLoginUser(user: API.LoginUserVO | null) {
      if (user) {
        this.userName = user.userName;
        this.avatarUrl = user.avatarUrl;
        this.phone = user.phone;
        this.email = user.email;
        this.id = user.id;
        this.createTime = user.createTime;
        this.updateTime = user.updateTime;
        this.openid = user.openid;
        this.userRole = user.userRole;
      } else {
        // 清空用户信息
        this.userName = undefined;
        this.avatarUrl = undefined;
        this.phone = undefined;
        this.email = undefined;
        this.id = undefined;
        this.createTime = undefined;
        this.updateTime = undefined;
        this.openid = undefined;
        this.userRole = undefined;
      }
    },
  },
  getters: {
    getUserName(): string | undefined {
      return this.userName;
    },
    getAvatarUrl(): string | undefined {
      return this.avatarUrl;
    },
    getPhone(): string | undefined {
      return this.phone;
    },
    getEmail(): string | undefined {
      return this.email;
    },
    getLoginUser(): API.LoginUserVO {
      return {
        userName: this.userName,
        avatarUrl: this.avatarUrl,
        phone: this.phone,
        email: this.email,
        id: this.id,
        createTime: this.createTime,
        updateTime: this.updateTime,
        openid: this.openid,
        userRole: this.userRole
      };
    },
  },
});
